package zero.tech.games.logic.common;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSONObject;

import zero.tech.games.common.GameConfig;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.common.data.RoomNode;
import zero.tech.games.entity.games.Club;
import zero.tech.games.logic.data.RoomStatus;
import zero.tech.wsnet.controller.LogicController;

public class Room {
	private int game_id;//游戏id
	private String room_id;//房间id
	private int room_status;//房间状态
	private int room_times;//房间局数
	private final int max_player;//最大人数
	private Club club;//俱乐部
	private long create_time=System.currentTimeMillis();//创建时间
	private Long nobody_time;//没人的时间
	private String configStr;//游戏配置
	private GameConfig gameConfig;
	private final Map<Integer, Integer> seats = new HashMap<Integer, Integer>();
	private final Map<Integer, LogicController> controllers = new ConcurrentHashMap<Integer, LogicController>();
	private GameBase gameBase;
	private RoomCache roomCache;
	private Timer timer;//房间计时器
	private Object transform_lock=new Object();
	private Object join_lock=new Object();
	private long queue_id;//队列id
	private final RoomNode roomNode;//房间节点所在redis信息
	private final int room_type;//0非匹配房间  1匹配房间

	public Room(String host, String port, String service_id, String configStr,String room_id,long queue_id,int game_id, Club club,int room_type) {
		GameConfig gameConfig=JSONObject.parseObject(configStr, GameConfig.class);
		this.room_type=room_type;
		this.gameConfig=gameConfig;
		this.game_id=game_id;
		this.configStr=configStr;
		this.queue_id=queue_id;
		this.room_id=room_id;
		this.room_status=RoomStatus.DEFAULT;
		this.room_times=0;
		this.club=club;
		this.max_player=gameConfig.getMax_player();
		for(int i=1;i<=max_player;i++) {
			this.seats.put(i, null);
		}
		RoomNode roomNode=new RoomNode();
		roomNode.setHost(host);
		roomNode.setPort(port);
		roomNode.setService_id(service_id);
		roomNode.setRoom_id(room_id);
		roomNode.setMax_player(max_player);
		roomNode.setGame_id(game_id);
		roomNode.setConfigStr(configStr);
		roomNode.setName(gameConfig.getName());
		roomNode.setPipei_key(gameConfig.getPipei_key());
		if(club==null) {
			roomNode.setClub_id(RedisConsts.A_DEFAULTCLUB);
		}else {
			roomNode.setClub_id(String.valueOf(club.getClub_id()));
		}
		this.roomNode=roomNode;
	}

	public String getRoom_id() {
		return room_id;
	}

	public void setRoom_id(String room_id) {
		this.room_id = room_id;
	}

	public int getRoom_status() {
		return room_status;
	}

	public void setRoom_status(int room_status) {
		this.room_status = room_status;
	}

	public int getRoom_times() {
		return room_times;
	}

	public void setRoom_times(int room_times) {
		this.room_times = room_times;
	}


	public GameBase getGameBase() {
		return gameBase;
	}

	public void setGameBase(GameBase gameBase) {
		this.gameBase = gameBase;
	}

	public RoomCache getRoomCache() {
		return roomCache;
	}

	public void setRoomCache(RoomCache roomCache) {
		this.roomCache = roomCache;
	}

	public Long getQueue_id() {
		return queue_id;
	}

	public void setQueue_id(Long queue_id) {
		this.queue_id = queue_id;
	}

	public Timer getTimer() {
		return timer;
	}

	public void setTimer(Timer timer) {
		this.timer = timer;
	}


	public Map<Integer, Integer> getSeats() {
		return seats;
	}

	public Map<Integer, LogicController> getAllControllers() {
		return controllers;
	}
	
	public Map<Integer, LogicController> getControllersWithoutWatcherAndTaotai() {
		RoomCache roomCache = this.getRoomCache();
		Set<Integer> watchers=roomCache.getWatchers();
		Set<Integer> taotais=roomCache.getTaotais();
		Map<Integer,LogicController> map = new HashMap<>();
		for(Integer uid:this.controllers.keySet()) {
			if(!watchers.contains(uid)&&!taotais.contains(uid)) {
				map.put(uid, this.controllers.get(uid));
			}
		}
		return map;
	}
	
	public Map<Integer, LogicController> getWatcherAndTaotai() {
		RoomCache roomCache = this.getRoomCache();
		Set<Integer> watchers=roomCache.getWatchers();
		Set<Integer> taotais=roomCache.getTaotais();
		Map<Integer,LogicController> map = new HashMap<>();
		for(Integer uid:this.controllers.keySet()) {
			if(watchers.contains(uid)||taotais.contains(uid)) {
				map.put(uid, this.controllers.get(uid));
			}
		}
		return map;
	}
	
	public Map<Integer, LogicController> getControllersWithoutTaotai() {
		RoomCache roomCache = this.getRoomCache();
		Set<Integer> taotais=roomCache.getTaotais();
		Map<Integer,LogicController> map = new HashMap<>();
		for(Integer uid:this.controllers.keySet()) {
			if(!taotais.contains(uid)) {
				map.put(uid, this.controllers.get(uid));
			}
		}
		return map;
	}
	
	
	public Map<Integer, LogicController> getControllersWithoutAdvance() {
		RoomCache roomCache = this.getRoomCache();
		Set<Integer> advances=roomCache.getAdvances();
		Map<Integer,LogicController> map = new HashMap<>();
		for(Integer uid:this.controllers.keySet()) {
			if(!advances.contains(uid)) {
				map.put(uid, this.controllers.get(uid));
			}
		}
		return map;
	}
	
	public Map<Integer, LogicController> getControllersWithoutWather() {
		RoomCache roomCache = this.getRoomCache();
		Set<Integer> wathers=roomCache.getWatchers();
		Map<Integer,LogicController> map = new HashMap<>();
		for(Integer uid:this.controllers.keySet()) {
			if(!wathers.contains(uid)) {
				map.put(uid, this.controllers.get(uid));
			}
		}
		return map;
	}
	
	
//	public Map<Integer, LogicController> getWatchers() {
//		RoomCache roomCache = this.getRoomCache();
//		Set<Integer> watchers=roomCache.getWatchers();
//		Map<Integer,LogicController> map = new HashMap<>();
//		for(Integer uid:this.controllers.keySet()) {
//			if(watchers.contains(uid)) {
//				map.put(uid, this.controllers.get(uid));
//			}
//		}
//		return map;
//	}
//	
//	public Map<Integer, LogicController> getTaotais() {
//		RoomCache roomCache = this.getRoomCache();
//		Set<Integer> taotais=roomCache.getTaotais();
//		Map<Integer,LogicController> map = new HashMap<>();
//		for(Integer uid:this.controllers.keySet()) {
//			if(taotais.contains(uid)) {
//				map.put(uid, this.controllers.get(uid));
//			}
//		}
//		return map;
//	}



	public String getConfigStr() {
		return configStr;
	}

	public void setConfigStr(String configStr) {
		this.configStr = configStr;
	}


	public Object getTransform_lock() {
		return transform_lock;
	}

	public void setTransform_lock(Object transform_lock) {
		this.transform_lock = transform_lock;
	}

	public RoomNode getRoomNode() {
		return roomNode;
	}

	public Club getClub() {
		return club;
	}

	public void setClub(Club club) {
		this.club = club;
	}

	public void setQueue_id(long queue_id) {
		this.queue_id = queue_id;
	}

	public long getCreate_time() {
		return create_time;
	}

	public void setCreate_time(long create_time) {
		this.create_time = create_time;
	}

	public int getGame_id() {
		return game_id;
	}

	public void setGame_id(int game_id) {
		this.game_id = game_id;
	}

	public int getMax_player() {
		return max_player;
	}

	public GameConfig getGameConfig() {
		return gameConfig;
	}

	public void setGameConfig(GameConfig gameConfig) {
		this.gameConfig = gameConfig;
	}

	public Long getNobody_time() {
		return nobody_time;
	}

	public void setNobody_time(Long nobody_time) {
		this.nobody_time = nobody_time;
	}

	public Object getJoin_lock() {
		return join_lock;
	}

	public void setJoin_lock(Object join_lock) {
		this.join_lock = join_lock;
	}

	public int getRoom_type() {
		return room_type;
	}

	public boolean isWatcher(int uid) {
		RoomCache roomCache = this.getRoomCache();
		return roomCache.getWatchers().contains(uid);
	}

}
